<?php include 'header.php'; ?>
<?php include '../settings.php'; ?>
<?php
$Sura=$_POST['SuraNum'];
$con=mysqli_connect($host,$dbuserW,$dbpassW,$dbname);

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

var_dump($_POST);
var_dump($_POST['Aya']);

if ($_POST['Delete']=='delete')
{
echo "\nDelete\n";
foreach ($_POST['dels'] as $delpair)
{
$arraydelpair=explode(",",$delpair);
$catid=$arraydelpair[0];
$ayaid=$arraydelpair[1];
if ($firstayaid==0) $firstayaid=$ayaid;
$deleterelationsql="delete from CategoryAya where CategoryIndex='".$catid."' and AyaIndex='".$ayaid."'";
echo $deleterelationsql."\n";

$res=mysqli_query($con,$deleterelationsql);
$errors=mysqli_error($con);
echo("<br>Errorno=".$errors."<br>");
if (mysqli_errno($con)) 
{
echo("-------Delete Failed------");
$Cat="";
}

//echo "dd";
}

echo("<a href='".$adminlocation."sura.php?Sura=".$Sura."#".$firstayaid."'>Back to Form</a>");
exit(0);
}

//move all contents of one category into another and del the original 
if (strlen($_POST['ChangeCatTotal'])==36)
{
$changedcat=$_POST['ChangeCatTotal'];
//echo "blog".$changedcat;
$changetocat=$_POST["CategorySelected"];
if (strlen($changetocat)<>36)
{
echo "There is not valid catetory to move to";
exit(0);
}
//this part is not complete this is where we move cat contents and delete old cat


$tCat=mysqli_query($con,"Select * from CategoryAya where CategoryIndex='".$changedcat."';");
//var_dump($tCat);
//$ttCat=mysql_fetch_assoc($tCat);
//var_dump($ttCat);
//$Cat=$ttCat["newval"];
while($row = mysqli_fetch_array($tCat)) 
{
echo("<br>");
//echo($row."<br>");
//$Cat=$row[newval];
//add old data into new category
$movesql="insert into CategoryAya values ('".$changetocat."','".$row[AyaIndex]."','".$row[Sura]."','".$row[Aya]."','".$row[Banned]."')";
$newres=mysqli_query($con,$movesql);
}
//Delete old data
$newres=mysqli_query($con,"Delete from CategoryAya where CategoryIndex='".$changedcat."';");
//Dekete old Category
$newres=mysqli_query($con,"Delete from Category where CIndex='".$changedcat."';");



exit(0);
}


//move all contents of one category into another and del the original 
if (strlen($_POST['Changecatdesc'])==36)
{
$changedcat=$_POST['Changecatdesc'];
//echo "blog".$changedcat;
$changetocatname=$_POST["NewCategory"];
if (strlen($changetocatname)<3)
{
echo "There is not valid catetory to move to";
exit(0);
}
//this part is not complete this is where we move cat contents and delete old cat

$qr1="update Category set Description='".$changetocatname."' where CIndex='".$changedcat."';";
echo "<br>".$qr1."<br>";
$t1Cat=mysqli_query($con,$qr1);
var_dump($t1Cat);
//$ttCat=mysql_fetch_assoc($tCat);
//var_dump($ttCat);
//$Cat=$ttCat["newval"];




exit(0);
}

//change category 
if ($_POST['ChangeCat']=='changecat')
{
echo "\nChange Category\n";


if (strlen($_POST["CategorySelected"])>0) $Cat=$_POST["CategorySelected"];
else
{
echo("Nothing to do since there is no Category");
exit(0);
}

foreach ($_POST['dels'] as $delpair)
{
$arraydelpair=explode(",",$delpair);
$catid=$arraydelpair[0];
$ayaid=$arraydelpair[1];
if ($firstayaid==0) $firstayaid=$ayaid;
$deleterelationsql="Update  CategoryAya set CategoryIndex='".$Cat."' where CategoryIndex='".$catid."' and AyaIndex='".$ayaid."'";
echo $deleterelationsql."\n";

$res=mysqli_query($con,$deleterelationsql);
$errors=mysqli_error($con);
echo("<br>Errorno=".$errors."<br>");
if (mysqli_errno($con)) 
{
echo("-------Category transfer Failed------");
$Cat="";
}



//echo "dd";
}

echo("<a href='".$adminlocation."sura.php?Sura=".$Sura."#".$firstayaid."'>Back to Form</a>");
exit(0);
}

//end change category


if ($_POST['BanCat']=='bancat')
{
echo "\nBan\n";
foreach ($_POST['dels'] as $delpair)
{
$arraydelpair=explode(",",$delpair);
$catid=$arraydelpair[0];
$ayaid=$arraydelpair[1];
if ($firstayaid==0) $firstayaid=$ayaid;
$deleterelationsql="Update  Category set Banned=1 where CIndex='".$catid."' ";
echo $deleterelationsql."\n";

$res=mysqli_query($con,$deleterelationsql);
$errors=mysqli_error($con);
echo("<br>Errorno=".$errors."<br>");
if (mysqli_errno($con)) 
{
echo("-------Ban Failed------");
$Cat="";
}


//echo "dd";
}

if (strlen($_POST["CategorySelected"])>0)
{
$catid=$_POST["CategorySelected"];
$deleterelationsql="Update  Category set Banned=1 where CIndex='".$catid."' ";
echo $deleterelationsql."\n";

$res=mysqli_query($con,$deleterelationsql);
$errors=mysqli_error($con);
echo("<br>Errorno=".$errors."<br>");
if (mysqli_errno($con)) 
{
echo("-------Ban Failed------");
$Cat="";
}
}

echo("<a href='".$adminlocation."sura.php?Sura=".$Sura."#".$firstayaid."'>Back to Form</a>");
exit(0);
}


If (strlen($_POST['NewCategory'])>0) 
{

$Catdesc=$_POST['NewCategory'];
$tCat=mysqli_query($con,"Select UUID() as newval;");
//var_dump($tCat);
//$ttCat=mysql_fetch_assoc($tCat);
//var_dump($ttCat);
//$Cat=$ttCat["newval"];
while($row = mysqli_fetch_array($tCat)) 
{
echo("<br>");
echo($row[newval]."<br>");
$Cat=$row[newval];
}

echo("NewCategory=".$Cat);
$theinsert="Insert into Category Value ('".$Cat."','".$Catdesc."','','','0');";
$res=mysqli_query($con,$theinsert);
echo($theinsert);
$errors=mysqli_error($con);
echo("<br>Errorno=".$errors."<br>");
if (mysqli_errno($con)) 
{
echo("-------Failed Insert------");
$Cat="";
}
}
elseif (strlen($_POST["CategorySelected"])>0) $Cat=$_POST["CategorySelected"];
else
{
echo("Nothing to do since there is no Category");
}
echo("<br>");
if($Cat<>"")
{
foreach ($_POST['Aya'] as $AyaVals)
{
if ($firstayavals==0) $firstayavals=$AyaVals;
$alls="'".$Cat."'".",".$AyaVals.",'0'";
echo($alls."</br>");
$res = mysqli_query($con,"Insert into CategoryAya value (".$alls.") ;");
if($res==null) $res="Did Not Work";
echo("Insert into CategoryAya value (".$alls.") ;<br>");

echo($res."</br>");
}
}
echo("<a href='".$adminlocation."sura.php?Sura=".$Sura."#".$firstayavals."'>Back to Form</a>");











mysqli_close($con);
?> 
<?php include 'footer.php'; ?>